-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday7 - part1.py
More file actions
55 lines (46 loc) · 1.23 KB
/
Copy pathday7 - part1.py
File metadata and controls
55 lines (46 loc) · 1.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
from functools import cache
file = open("input.txt")
lines = file.read().splitlines()
cwd = []
cur_dir_content = []
files = {}
folders = set()
folders.add("/")
for line in lines:
if line.startswith("$"):
if len(cur_dir_content) != 0:
files["/".join(cwd)] = cur_dir_content.copy()
cur_dir_content = []
if line[2:4] == "cd":
dir = line.split()[-1]
match dir:
case "/":
cwd = ["/"]
case "..":
cwd.pop()
case _:
cwd.append(dir)
else: # listing
size, name = line.split()
if size == "dir":
size = -1
name = "/".join(cwd + [name])
folders.add(name)
cur_dir_content.append((name, int(size)))
if len(cur_dir_content) != 0:
files["/".join(cwd)] = cur_dir_content.copy()
cur_dir_content = []
@cache
def get_size(dir):
total_size = 0
for file, size in files[dir]:
if size == -1:
size = get_size(file)
total_size += size
return total_size
folder_sum = 0
for folder in folders:
size = get_size(folder)
if size <= 100000:
folder_sum += size
print(folder_sum)